// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.ShipPlans;
import com.fowo.api.model.ship.plans.ShipPlansItemVo;
import com.fowo.api.model.ship.plans.ShipPlansSearchParamPo;
import com.fowo.api.model.ship.plans.ShipPlansVo;
import com.fowo.api.service.ShipPlansService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "发货计划单")
@RequestMapping("/api/ship-plans")
@RestController
public class ShipPlansController {

  @Resource
  private ShipPlansService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<ShipPlansItemVo>> search(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<ShipPlansItemVo>>> searchOptions(
    ShipPlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<ShipPlansItemVo>> getPage(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "获取包括子表的查询结果")
  @GetMapping("page-with-child")
  public R<Page<?>> getPageWithChild(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageWithChildSearch(search));
  }

  @Operation(summary = "仅获取子表的查询结果")
  @GetMapping("page-only-child/{parentId}")
  public R<Page<?>> getPageOnlyChild(
    @PathVariable Long parentId,
    ShipPlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageOnlyChildSearch(parentId, search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<ShipPlansVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取创建批次号相关的编号列表")
  @GetMapping("create-batch-number/{texts}/ids")
  public R<List<Long>> getIdsByCreateBatchNumber(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByCreateBatchNumber(nameList));
  }

  @Operation(summary = "获取编号相关的创建批次号列表")
  @GetMapping("create-batch-number/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getCreateBatchNumberByIds(idList));
  }

  @PreAuthorize("@am.canAccess('shipPlansCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody ShipPlansVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('shipPlansEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody ShipPlansVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('shipPlansEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody ShipPlansVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('shipPlansDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('shipPlansDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('shipPlansPrintFNSKUBarcodeOld')")
  @Operation(summary = "打印FNSKU条码（旧）")
  @PostMapping("printFNSKUBarcodeOld")
  public R printFNSKUBarcodeOld(@RequestBody IdListPo po) {
    return R.run(() -> service.printFNSKUBarcodeOld(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('shipPlansPrintFNSKUBarcodeCustom')")
  @Operation(summary = "打印FNSKU条码（自定义）")
  @PostMapping("printFNSKUBarcodeCustom")
  public R printFNSKUBarcodeCustom(@RequestBody IdListPo po) {
    return R.run(() -> service.printFNSKUBarcodeCustom(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('shipPlansPrintFNSKUBarcodeTemplate')")
  @Operation(summary = "打印FNSKU条码（模板）")
  @PostMapping("printFNSKUBarcodeTemplate")
  public R printFNSKUBarcodeTemplate(@RequestBody IdListPo po) {
    return R.run(() -> service.printFNSKUBarcodeTemplate(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('shipPlansPrintTheOdaiLabel')")
  @Operation(summary = "打印欧代标签")
  @PostMapping("printTheOdaiLabel")
  public R printTheOdaiLabel(@RequestBody IdListPo po) {
    return R.run(() -> service.printTheOdaiLabel(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('shipPlansPrintRegularShippingSchedule')")
  @Operation(summary = "打印常规发货计划")
  @PostMapping("printRegularShippingSchedule")
  public R printRegularShippingSchedule(@RequestBody IdListPo po) {
    return R.run(() -> service.printRegularShippingSchedule(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('shipPlansPrintTheCombinedShipmentPlan')")
  @Operation(summary = "打印组合发货计划")
  @PostMapping("printTheCombinedShipmentPlan")
  public R printTheCombinedShipmentPlan(@RequestBody IdListPo po) {
    return R.run(() -> service.printTheCombinedShipmentPlan(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('shipPlansPrintRegularShipmentPlanTemplate')")
  @Operation(summary = "打印常规发货计划（模板）")
  @PostMapping("printRegularShipmentPlanTemplate")
  public R printRegularShipmentPlanTemplate(@RequestBody IdListPo po) {
    return R.run(() -> service.printRegularShipmentPlanTemplate(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('shipPlansPrintCompositeShipmentPlanTemplate')")
  @Operation(summary = "打印组合发货计划（模板）")
  @PostMapping("printCompositeShipmentPlanTemplate")
  public R printCompositeShipmentPlanTemplate(@RequestBody IdListPo po) {
    return R.run(() -> service.printCompositeShipmentPlanTemplate(po.getIds()));
  }

  /**
   * 视图选项卡标题上显示计数
   */
  @Operation(summary = "视图选项卡标题上显示计数")
  @GetMapping("getTabHeaderShowTotal")
  public R<Map> getTabHeaderShowTotal() {
    return R.data(() -> service.getTabHeaderShowTotal());
  }

  @Operation(summary = "全部")
  @GetMapping("entire")
  public R<List<ShipPlansItemVo>> entire(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.entire(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("entirePage")
  public R<Page<ShipPlansItemVo>> entirePage(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.entirePage(search));
  }

  @Operation(summary = "全部包括子表的查询结果")
  @GetMapping("entirePage-with-child")
  public R<Page<?>> entirePageWithChild(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.entirePageWithChildSearch(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("entireLastUpdateTime")
  public R<Date> entireLastUpdateTime(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.entireLastUpdateTime(search));
  }

  @Operation(summary = "待处理")
  @GetMapping("pending")
  public R<List<ShipPlansItemVo>> pending(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pending(search));
  }

  @Operation(summary = "待处理分页")
  @GetMapping("pendingPage")
  public R<Page<ShipPlansItemVo>> pendingPage(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingPage(search));
  }

  @Operation(summary = "待处理包括子表的查询结果")
  @GetMapping("pendingPage-with-child")
  public R<Page<?>> pendingPageWithChild(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pendingPageWithChildSearch(search));
  }

  @Operation(summary = "待处理最后更新时间")
  @GetMapping("pendingLastUpdateTime")
  public R<Date> pendingLastUpdateTime(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingLastUpdateTime(search));
  }

  @Operation(summary = "已处理")
  @GetMapping("processed")
  public R<List<ShipPlansItemVo>> processed(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processed(search));
  }

  @Operation(summary = "已处理分页")
  @GetMapping("processedPage")
  public R<Page<ShipPlansItemVo>> processedPage(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processedPage(search));
  }

  @Operation(summary = "已处理包括子表的查询结果")
  @GetMapping("processedPage-with-child")
  public R<Page<?>> processedPageWithChild(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.processedPageWithChildSearch(search));
  }

  @Operation(summary = "已处理最后更新时间")
  @GetMapping("processedLastUpdateTime")
  public R<Date> processedLastUpdateTime(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processedLastUpdateTime(search));
  }

  @Operation(summary = "已驳回")
  @GetMapping("j-n-j")
  public R<List<ShipPlansItemVo>> jNJ(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.jNJ(search));
  }

  @Operation(summary = "已驳回分页")
  @GetMapping("j-n-jPage")
  public R<Page<ShipPlansItemVo>> jNJPage(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.jNJPage(search));
  }

  @Operation(summary = "已驳回包括子表的查询结果")
  @GetMapping("j-n-jPage-with-child")
  public R<Page<?>> jNJPageWithChild(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.jNJPageWithChildSearch(search));
  }

  @Operation(summary = "已驳回最后更新时间")
  @GetMapping("j-n-jLastUpdateTime")
  public R<Date> jNJLastUpdateTime(ShipPlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.jNJLastUpdateTime(search));
  }
}
